package com.example.check.web.controller;

import com.example.check.entity.PermissionEntity;
import com.example.check.service.PermissionServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import static com.example.common.AssertThrowUtil.$;

/**
 * Created by Tony Yao on 2017/7/17.
 */
@Controller
public class PermissionController {
    @Resource
    private PermissionServiceImpl pfun;

    @RequestMapping(method= RequestMethod.GET,path="/searchPermission.do")
    public String searchPermissionView(String field, String code, HttpServletRequest request , Model model) throws Exception {
        ArrayList<PermissionEntity> permissionList =null;
        if(code == null) {
            permissionList = pfun.searchAllPermission();
        }
        else{
            permissionList = pfun.searchPermissionLikeByField(field,code);
        }
        model.addAttribute("searchPermissionList",permissionList);
//        HttpSession session = request.getSession();
//        session.setAttribute("searchPermissionList", permissionList);
        return "searchPermission";
    }

    @RequestMapping(method= RequestMethod.GET,path="/deletePermission.do")
    public String deletePermissionView(int id, HttpServletRequest request)throws Exception{
        ArrayList<PermissionEntity> permissionList = null;
        pfun.deletePermissionById(id);
        permissionList = pfun.searchAllPermission();

        HttpSession session = request.getSession();
        session.setAttribute("permissionList", permissionList);
        request.setAttribute("message","删除权限成功！");
        return "managePermission";
    }

    @RequestMapping(method= RequestMethod.POST,path="/editPermission.do")
    public String editPermission(int id, String name, String description, HttpServletRequest request){
        try {
            name =$("名字不能为空！",name);
            PermissionEntity p = new PermissionEntity(id,name,description);
            pfun.editPermission(p);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ArrayList<PermissionEntity> permissionList = null;
        try {
            permissionList = pfun.searchAllPermission();
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpSession session = request.getSession();
        session.setAttribute("permissionList", permissionList);
        request.setAttribute("message","修改权限成功！");
        return "managePermission";
    }

    @RequestMapping(method= RequestMethod.GET,path="/editPermissionView.do")
    public String editPermissionView(int id, HttpServletRequest request){
        try {
            PermissionEntity chosenPermission = pfun.searchPermissionByID(id);
            HttpSession session = request.getSession();
            session.setAttribute("chosenPermission", chosenPermission);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "editPermission";
    }

    @RequestMapping(method= RequestMethod.GET,path="/addPermission.do")
    public String addPermission(String name, String description, HttpServletRequest request){
        try{
            name =$("名字不能为空！",name);
            PermissionEntity r = new PermissionEntity(-1,name,description);
            pfun.insertPermission(r);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ArrayList<PermissionEntity> permissionList = null;
        try {
            permissionList = pfun.searchAllPermission();
        } catch (Exception e) {
            e.printStackTrace();
        }
        HttpSession session = request.getSession();
        session.setAttribute("permissionList", permissionList);
        request.setAttribute("message","添加权限成功！");
        return "managePermission";
    }

    @RequestMapping(method= RequestMethod.GET,path="/addPermissionView.do")
    public String addPermissionView(){

        return "addPermission";
    }


    @RequestMapping(method= RequestMethod.GET,path="/managePermission.do")
    public String managePermissionView(String field, String code, HttpServletRequest request) throws Exception{
        ArrayList<PermissionEntity> permissionList =null;
        if(code == null) {
            permissionList = pfun.searchAllPermission();
        }
        else{
            permissionList = pfun.searchPermissionLikeByField(field,code);
        }
        HttpSession session = request.getSession();
        session.setAttribute("permissionList", permissionList);
        return "managePermission";
    }
}
